import 'package:dio/dio.dart';
import 'package:flutter_axrjc/datasource/network/bean/ArticleBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/ArticleTabBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/HelpCenterBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/HelpTabBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/SingleArticleBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/SingleHelpDetailBean.dart';
import 'package:flutter_axrjc/datasource/network/request/network_service.dart';

class KnowledgeApi {
  static Future<List<ArticleBean>> getTopThree({
    required String limit,
    required String page,
    required String categoryId,
    required bool ifText,
  }) async {
    var map = {"limit": limit, "offset": page, "ifText": "$ifText"};
    if (categoryId.isNotEmpty) {
      map['categoryId'] = categoryId;
    }
    var res = await NetworkService.post(
        url: 'app/proKnoewlegeArticlesQuery.do', map: map, showLoading: false);
    return res.getTypedList<ArticleBean>((map) => ArticleBean.fromJson(map)) ??
        [];
  }

  static Future<List<ArticleTabBean>> getTabs() async {
    var res = await NetworkService.post(
        url: 'app/proKnowlegeCategoriesQuery.do',
        map: {"limit": "100", "offset": "1"});
    return res.getTypedList<ArticleTabBean>(
            (map) => ArticleTabBean.fromJson(map)) ??
        [];
  }

  static Future<List<ArticleTabBean>> getTabsPage(
      {required String page}) async {
    var res = await NetworkService.post(
        url: 'app/proKnowlegeCategoriesQuery.do',
        map: {"limit": "10", "offset": page},
        showLoading: false);
    return res.getTypedList<ArticleTabBean>(
            (map) => ArticleTabBean.fromJson(map)) ??
        [];
  }

  static Future<List<HelpTabBean>> getHelpTabs() async {
    var res = await NetworkService.post(
        url: 'app/proHelpCategoriesQuery.do',
        map: {"limit": "100", "offset": "1"});
    return res.getTypedList<HelpTabBean>((map) => HelpTabBean.fromJson(map)) ??
        [];
  }

  static Future<SingleArticleBean?> getSingleArticle(
      {required String key}) async {
    var res = await NetworkService.post(
        url: "app/processKnoewlegeArticlesView/$key", map: {});
    return res.getTypedData((map) => SingleArticleBean.fromJson(map));
  }

  static Future post({
    required String categoryId,
    required List<String> imgFile,
    required String articleContent,
    required String articleTitle,
    required String file,
  }) async {
    List<MultipartFile> files = [];
    for (var element in imgFile) {
      files.add(await MultipartFile.fromFile(element));
    }
    var map = {
      "categoryId": categoryId,
      "imgFile": files,
      "articleTitle": articleTitle,
      "articleContent": articleContent,
    };

    if (file.isNotEmpty) {
      map['file'] = await MultipartFile.fromFile(file);
    }

    await NetworkService.post(
        url: "app/proKnoewlegeArticlesSave.do", map: map, useFormData: true);
  }

  static postHelp(
      {required String categoryId,
      required List<String> imgFile,
      required String articleContent,
      required String articleTitle,
      required String file}) async {
    List<MultipartFile> files = [];
    for (var element in imgFile) {
      files.add(await MultipartFile.fromFile(element));
    }
    var map = {
      "helpCategoriesId": categoryId,
      "imgFile": files,
      "contentTitle": articleTitle,
      "contentContent": articleContent,
    };

    if (file.isNotEmpty) {
      map['file'] = await MultipartFile.fromFile(file);
    }
    await NetworkService.post(
        url: "app/proHelpContentsSave.do", map: map, useFormData: true);
  }

  static Future<List<HelpCenterBean>> getHelpList(
      {required String limit,
      required String page,
      required String categoryId}) async {
    var res = await NetworkService.post(
        url: "app/proHelpContentsQuery.do",
        map: {
          "limit": limit,
          "offset": page,
          "helpCategoriesId": categoryId,
        },
        showLoading: false);
    return res.getTypedList<HelpCenterBean>(
            (map) => HelpCenterBean.fromJson(map)) ??
        [];
  }

  static Future<List<HelpCenterBean>> getMyListHelpList(
      {required String page}) async {
    var res = await NetworkService.post(
        url: "app/proHelpContentsQuery.do",
        map: {
          "limit": "10",
          "offset": page,
        },
        showLoading: false);
    return res.getTypedList<HelpCenterBean>(
            (map) => HelpCenterBean.fromJson(map)) ??
        [];
  }

  static Future<void> deletiSingleHelp(String s) async {
    await NetworkService.post(url: "app/proDelHelpContents/$s", map: {});
  }

  static Future<SingleHelpDetailBean?> getSingleHelp(String s) async {
    var res = await NetworkService.post(
        url: "app/processHelpContentsView/$s", map: {});
    return res.getTypedData((map) => SingleHelpDetailBean.fromJson(map));
  }

  static Future<List<ArticleBean>> getKnowledgeList(
      {required String page}) async {
    var res = await NetworkService.post(
        url: "app/proKnoewlegeArticlesQuery.do",
        map: {
          "limit": "10",
          "offset": page,
        },
        showLoading: false);
    return res.getTypedList<ArticleBean>((map) => ArticleBean.fromJson(map)) ??
        [];
  }

  static deleteKnowledge(String s) async {
    await NetworkService.post(url: "app/proDelKnoewlegeArticles/$s", map: {});
  }

  static Future changeArticle(
      {required String articelHomePic,
      required String articleContent,
      required String articleTitle,
      required String articleId,
      required List<String> imgFile}) async {
    List<MultipartFile> files = [];
    for (var element in imgFile) {
      files.add(await MultipartFile.fromFile(element));
    }
    await NetworkService.post(
        url: "/app/proKnoewlegeArticlesUpdate.do",
        map: {
          "articelHomePic": articelHomePic,
          "articleContent": articleContent,
          "articleTitle": articleTitle,
          "articleId": articleId,
          "imgFile": files
        },
        useFormData: true);
  }

  static Future changeHelp(
      {required String articelHomePic,
      required String articleContent,
      required String articleTitle,
      required String articleId,
      required List<String> imgFile}) async {
    List<MultipartFile> files = [];
    for (var element in imgFile) {
      files.add(await MultipartFile.fromFile(element));
    }
    await NetworkService.post(
        url: "/app/proHelpContentsUpdate.do",
        map: {
          "articelHomePic": articelHomePic,
          "contentContent": articleContent,
          "contentTitle": articleTitle,
          "contentId": articleId,
          "imgFile": files
        },
        useFormData: true);
  }
}
